<?php
class Yu_editorController extends ManagerController {
	public function Yu_editorController(){
		parent::ManagerController();
		define('WIDGET_DIR', ROOT_PATH . '/protected/includes/widgets/');
	}
	
	public function layout(){
		$this->display();
	}
	
	public function widget(){
		//获取挂件分类
		$cates = self::get_widget_cates();
		$this->assign('cates', $cates);
		$this->display();
	}
	
	private function get_widget_cates(){
		$relation_mod = new RelationModel();
		$cates = $relation_mod->get_relation_by_code('widget_cate');
		lang_fatch($cates, LANGUAGE);
		//删除英文包
		foreach ($cates as &$widget_cate){
			if(isset($widget_cate['en'])){
				unset($widget_cate['en']);
			}
		}
		
		//加载组件分类下的组件
		$widget_mod = new WidgetModel();
		$widgets = $widget_mod->find(null, 'cate,code,name,templates');
		foreach ($widgets as $widget){
			$cate = $widget['cate'];
			$code = $widget['code'];
			$w = array(
				'name' => $widget['name'],
				'desc' => isset($widget['desc'])?$widget['desc']:'',
			);
			foreach ($widget['templates'] as $template){
				$w['templates'][$template['template_file']] = array(
					'name' => $template['template_name'],
					'desc' => $template['template_desc'],
					'thumb' => $template['template_thumb'],
					'file' => $template['template_file'],
				);
			}
			
			$cates[$cate]['items'][$code] = $w;
		}
		return $cates;
		
// 		print_r($cates);exit;
// 		$cates = array(
// 			'common' => array('name'=>'常规组件'),
// 			'adv' => array('name'=>'广告组件'),
// 			'goods' => array('name'=>'商品组件'),
// 			'article' => array('name'=>'文章组件'),
// 			'order' => array('name'=>'订单组件'),
// 			'private' => array('name'=>'私有组件'),
// 		);
// 		$themlate_thumb_path = ROOT_PATH . '/template/widgets/';
// 		foreach ($cates as $cate_key => &$cate){
// 			if(!is_dir(WIDGET_DIR . $cate_key))continue;
// 			$widget_cates = array();
// 			$except_dirs = array('.', '..', '.svn', 'layout', 'root');
// 			$handle = opendir(WIDGET_DIR . $cate_key);
// 			while (false !== ($file = readdir($handle))) {
// 				if(!in_array($file, $except_dirs)) {
// 					$cur_path = WIDGET_DIR . $cate_key . '/' . $file;
// 					if(is_dir($cur_path)){
// 						$widget_class_name = ucfirst($file) . 'Widget';
// 						if(is_file($cur_path . '/' . $widget_class_name . '.php')){
// 							require_once $cur_path . '/' . $widget_class_name . '.php';
// 							$widget = new $widget_class_name;
// 							$cate['items'][$file] = $widget->get_info();
							
// 							//读取模板
// 							$template_handle = opendir($cur_path . '/template/');
// 							while (false !== ($template = readdir($template_handle))) {
// 								if(in_array($template, $except_dirs))continue;
// 								$template_name = preg_replace('/^(.*)\.' . TEMPLATE_TYPE . '$/im', '$1', $template);
// 								$template_file = $cur_path . '/template/' . $template;
// 								if(is_file($template_file)){
// 									//获取缩略图
// 									$template_thumb = $themlate_thumb_path . $cate_key . '/' . $file . '/' . $template_name . '.png';
// 									if(is_file($template_thumb)){
// 										$cate['items'][$file]['templates'][$template] = 'widgets/' . $cate_key . '/' . $file . '/' . $template_name . '.png';
// 									}else{
// 										$cate['items'][$file]['templates'][$template] = 'manager/images/no_picture.png';
// 									}
// 								}
// 							}
// 							closedir($template_handle);
// 						}
// 					}
// 				}
// 			}
// 			closedir($handle);
// 		}
	}
	
	public function add_widget(){
		$widget_type = self::get_post_val('type', 'layout');
		$widget_name = self::get_post_val('widget_name');
		$widget_cate = self::get_post_val('widget_cate');
		$widget_template = self::get_post_val('widget_template');
		$template_file_path = WIDGET_DIR . $widget_cate . '/' . $widget_name . '/template/' . $widget_template . '.htm';
		$contner = self::get_post_val('contner');
		$parent = self::get_post_val('parent');
		
		$node = array(
			'template' => $widget_template, //组件模板
			'cate' => $widget_cate,			//组件分类
			'type' => $widget_type, 		//组件类型（layout | widget | cell）
			'name' => $widget_name,			//组件名称
			'rank' => 10000,
			'id' => gen_guid(),
		);
		self::add_widget_and_save($node, $parent, $contner);
		
		$html = widget_display($node);
		echo json_encode(array(
				'result' => true,
				'data' => $html,
		));
	}
	
	/**
	 * 本处添加挂件是不包含配置的，使用默认配置
	 */
	private function add_widget_and_save(&$node, $parent_id, $contner=null){
		$tree = self::init_tree();
		switch ($node['type']){
			case 'layout':
				//计算是多少栏的
				$n = intval(preg_replace('/layout_(\d)/i', '$1', $node['name']));
				for($i=1;$i<=$n;++$i){
					$node['config']['id' . $i] = gen_guid();
				}
// 				$tree->add_node($node, $parent_id, $contner);
				break;
			default :
				//普通组件
				break;
		}
		$tree->add_node($node, $parent_id, $contner);
		$tree->save();
	}
	
	private function init_tree(){
		$tree = null;
		if(isset($_REQUEST['tpl'])){
			$tpl = $_REQUEST['tpl'];
			$tree = new PageTree($tpl);
		}else{
			$name = $_REQUEST['_app'] . '_' . $_REQUEST['_act'];
			$tree = new PageTree();
			$tree->init_page_by_name($name);
		}
		return $tree;
	}
	
	public function del_layout(){
		$tree = self::init_tree();
		
		$del_node = $tree->get_item($this->id);
		//抽取里面的非布局挂件	
		$tree->remove_node($this->id);
		$tree->save();
		self::ajax_info("删除成功！");
// 		$node_str = var_export($del_node, true);
	}
	
	public function style_save(){
		$tree = self::init_tree();
		$node = &$tree->get_item($this->id);
		$styles = json_decode(self::get_post_val('data'), true);
// 		if(isset($node['styles'])){
// 			$node['styles'] = array_merge($node['styles'], $styles);
// 		}else{
// 			$node['styles'] = $styles;
// 		}
		$tree->add_style($this->id, $styles);
		$node = &$tree->get_item($this->id);
		//重新生成文件
// 		$tree->set_node($node);
		$tree->save();
		self::ajax_info('保存成功！');
		$ss = var_export($node, true);
		self::ajax_error($ss);
		print_r($styles);exit;
	}
}

?>